package  net.libra.ui
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 * @author 姜波
	 * @createTime 2011-1-14
	 * @version 1.1
	 * */
	public class Slider extends EventDispatcher
	{
		protected var _maximum:Number = 100;
		protected var _minimum:Number = 0;
		protected var _value:Number = 0;
		protected var _stepSize:Number = 1;
		protected var _orientation:String;
		
		public static const HORIZONTAL:String = "horizontal";
		public static const VERTICAL:String = "vertical";
		public static const DEFAULT_SIZEA:Number = 10;
		public static const DEFAULT_SIZEB:Number = 100;
		
		protected var _button:EasyButton;
		private var _skin:MovieClip;
		private var _stage:Stage;
		//protected var _buttonSkin:ISkin;
		
		public function Slider(stage:Stage,button:EasyButton,skin:MovieClip,min:Number, max:Number,stepSize:Number,orientation:String = Slider.HORIZONTAL)
		{
			_stage = stage;	
			_button = button;
			_skin = skin;
			_orientation = orientation;
			
			_minimum = min;
			_maximum = max;
			
			_stepSize = stepSize;
			
			_button.addEventListener(MouseEvent.MOUSE_DOWN,button_MouseDownHandler);
			setButtonLocation();
			init();
		}

		protected function init():void
		{
			this.value = this.minimum;
			
			//设置_button初始化位置.
			if(_orientation == HORIZONTAL)
			{
				_button.x = 0;
			}else{
				_button.y = skin.height - _button.height;
			}
			
			setStepPixel();
		}
		
		protected function button_MouseDownHandler(e:MouseEvent):void
		{
			_stage.addEventListener(MouseEvent.MOUSE_UP,stage_MouseUpHandler);
			_stage.addEventListener(MouseEvent.MOUSE_MOVE,stage_MouseMoveHander);
		}
		protected function stage_MouseUpHandler(e:MouseEvent):void
		{
			_stage.removeEventListener(MouseEvent.MOUSE_UP,stage_MouseUpHandler);
			_stage.removeEventListener(MouseEvent.MOUSE_MOVE,stage_MouseMoveHander);
		}
		public function destory():void
		{
			_stage.removeEventListener(MouseEvent.MOUSE_UP,stage_MouseUpHandler);
			_stage.removeEventListener(MouseEvent.MOUSE_MOVE,stage_MouseMoveHander);
			_stage = null;
			_button.removeEventListener(MouseEvent.MOUSE_DOWN,button_MouseDownHandler);
			_button.destroy();
			_button = null;
			_skin = null;
		}
		protected function setButtonLocation():void
		{
			if(_orientation == HORIZONTAL)
			{
				_button.y -= 0;
			}else{
				_button.x -= 0;
			}
		}
		
		private var lastX:Number = 0;
		/**
		 *步进算法
		 */
		protected function stage_MouseMoveHander(e:MouseEvent):void{
			
			var valueMouseX:Number = skin.mouseX;
			var valueMouseY:Number = skin.mouseY;
			if(_orientation == HORIZONTAL)
			{
				if(valueMouseX < 0 || valueMouseX > skin.width) return;
				
				var flag:Number = valueMouseX - lastX; 
				if(flag > 0){//鼠标在button的右面
					setButtonX(valueMouseX,"right");
				}else{//鼠标在button的左面
					setButtonX(valueMouseX,"left");
				}
			}else{
				
			}
			lastX = valueMouseX;
		}
		//value算法版本1-------没用上
		protected function setValue(val:Number):void
		{
			_value =  Math.round((_minimum +(_maximum - _minimum)*val/skin.height));
			dispatchEvent(new Event(Event.CHANGE));
		}
		//value算法版本2
		protected function setValue2(type:String):void
		{
			switch(type){
				case "ADD":
					_value += stepSize;
					dispatchEvent(new Event(Event.CHANGE));
					break;
				case "SUB":
					_value -= stepSize;
					dispatchEvent(new Event(Event.CHANGE));
					break;
				case "MAX":
					_value = _maximum;
					dispatchEvent(new Event(Event.CHANGE));
					break;
				case "MIN":
					_value = _minimum;
					dispatchEvent(new Event(Event.CHANGE));
					break;
			}
		}
		private var _currentStep:int = 0;
		private var lastFlag:Boolean;//是否最后一份特殊处理
		//总份数
		private function getTotalStep():int
		{
			var tempNum:Number = (maximum - minimum)%stepSize;
			if(tempNum == 0)
			{
				lastFlag = false;
				return (maximum - minimum)/stepSize;
			}else{
				lastFlag = true;
				return (maximum - minimum)/stepSize+1;
			}
		}
		private var commonPixel:Number;
		private var lastStepPixel:Number;//最后不够或者够的一份
		public function set value(value:Number):void
		{
			var fenNum:int;
			//判断value是最后一份
			if(value >= maximum)
			{
				fenNum = getTotalStep();
				_button.x = skin.width - _button.width;
			
				_currentStep = fenNum;
				_value = maximum;
			}else if(value <= minimum)
			{
				_button.x = 0;
				_currentStep = 0;
				_value = minimum;
			}else{
				if((value - minimum)%stepSize == 0)
				{
					fenNum = (value - minimum)/stepSize + 1;
				}else{
					if((value - minimum)%stepSize < stepSize/2)
					{
						fenNum = (value - minimum)/stepSize;
					}else{
						fenNum = (value - minimum)/stepSize + 1;
					}
				}
				
				
				_currentStep = fenNum;
				
				if(_currentStep == getTotalStep())
				{
					_button.x = skin.width - _button.width;
					_value = maximum;
				}else{
					_button.x = commonPixel * fenNum;
					_value = minimum + (_currentStep * stepSize);
				}
				
			}
			
			dispatchEvent(new Event(VALUE_CHANGE));
		}
		public static var VALUE_CHANGE:String = "value_change";
		//一份有多少个像素
		private function setStepPixel():void
		{
			getTotalStep();
			commonPixel = stepSize*(skin.width - _button.width)/(maximum - minimum);
			if(lastFlag == true){
				lastStepPixel = ((skin.width - _button.width) - commonPixel*(getTotalStep() - 1));
			}else{
				lastStepPixel = commonPixel;
			}
		
		}
		public function get currentStep():int
		{
			return _currentStep;
		}
		private function setButtonX(mouseX:Number,flag:String):void
		{
			var total:int = getTotalStep();
			var pixelVlaue:Number = mouseX * (maximum - minimum)/(skin.width - _button.width);
			switch(flag)
			{
				case "right":
					if(_currentStep == total) return;
					
					this.value = minimum + pixelVlaue;
					break;
				case "left":
					if(_currentStep == 0) return;

					this.value = minimum + pixelVlaue;
					break;
			}
		}
		
		/**getter,setters**/
		
		public function get maximum():Number
		{
			return _maximum;
		}
		
		public function setMaximum(value:Number,runtime:Boolean):void
		{
			if (value == _maximum)
				return;
			
			_maximum = value;
			if(runtime)
			{
				this.value = _minimum;
			}
		}
		
		public function get minimum():Number
		{
			return _minimum;
		}
		
		public function set minimum(value:Number):void
		{
			if (value == _minimum)
				return;
			
			_minimum = value;
			_value = _minimum;
		}
		
		public function get value():Number
		{
			return _value;
		}
		public function get stepSize():Number
		{
			return _stepSize;
		}
		
		public function set stepSize(value:Number):void
		{
			if (value == _stepSize)
				return;
			
			_stepSize = value;
//			updateSize();
			
		}
		
		public function get skin():MovieClip
		{
			return _skin;
		}
		public function get button():EasyButton
		{
			return _button;
		}
		
		public function get orientation():String
		{
			return _orientation;
		}
	}
}